package abc.com.abc.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import abc.com.abc.R;
import abc.com.abc.utils.MD5Utils;

public class RegisterActivity extends AppCompatActivity {
      private TextView tv_main_title;//标题
    private TextView tv_back;//返回按钮
    private Button btn_register;//注册按钮
    private EditText et_username,et_psw;//用户名密码输入框
    private  String  userNmae,psw;//用户名密码的获取值
    private RelativeLayout rl_title_bar;//标题布局

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏
        init();
    }

    private void init() {
        tv_main_title=(TextView)findViewById(R.id.tv_main_title);
        tv_main_title.setText("注册");
        tv_back = (TextView)findViewById(R.id.tv_back);
        rl_title_bar = (RelativeLayout)findViewById(R.id.title_Bar);
        rl_title_bar.setBackgroundColor(Color.TRANSPARENT);
        btn_register = (Button)findViewById(R.id.bt_register);
        et_username = (EditText)findViewById(R.id.et_username);
        et_psw=(EditText)findViewById(R.id.et_password);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.this.finish();
            }
        });
       btn_register.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               //获取输入的字符串
               getEditString();
               if (TextUtils.isEmpty(userNmae)){
                   Toast.makeText(RegisterActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                   return;
               }else if (TextUtils.isEmpty(psw)){
                   Toast.makeText(RegisterActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                   return;
               }else if (isExistUserName(userNmae)){
                   Toast.makeText(RegisterActivity.this,"此账号名已经存在",Toast.LENGTH_SHORT).show();
                   return;
               }else {
                   Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                   saveRegisterInfo(userNmae,psw);
                       Intent data = new Intent();
                       data.putExtra("userName",userNmae);
                       setResult(RESULT_OK,data);
                       RegisterActivity.this.finish();
                   }
               }
           });
    }

     /*  获取控件中的字符串
            * */
   private void getEditString() {
               userNmae = et_username.getText().toString().trim();
               psw = et_psw.getText().toString().trim();


   }


/*判断用户名是否存在*/
    private boolean isExistUserName(String userNmae) {
        boolean has_userName = false;
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw = sp.getString(userNmae, "");
        if (!TextUtils.isEmpty(spPsw)) {
            has_userName = true;
        }
        return has_userName;
    }
        /*
        * 保存用户名和密码到SHAREDPREFERENCES;
        * */
    private void saveRegisterInfo(String userNmae, String psw) {
        String md5Psw = MD5Utils.md5(psw);//把文件用MD5加密
        //loginInfo表示文件名
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();//获取编辑器
        //保存到Sharedpreferendces
        editor.putString(userNmae,md5Psw);
        editor.commit();//提交修改


        }

}
